#include <iostream>
#include <opencv2/opencv.hpp>
#include <Eigen/Dense>
#include <time.h>
#include<algorithm>
#include<vector>
#include <bits/stdc++.h>
using namespace cv;
using namespace std;
double find(vector<double>&v)
{
double max1;
    vector<double>::iterator biggest = max_element(v.begin(), v.end());
   cout << "Max element is " << *biggest<< " at position " << distance(v.begin(), biggest) << endl;
  
    auto smallest = min_element(v.begin(), v.end());
    cout << "min element is " << *smallest<< " at position " << distance(v.begin(), smallest) << endl;
max1=*biggest;
return max1;
}

void printvector(vector<double>&v)

{
    for (vector<double>::iterator it =  v.begin(); it !=v.end();it++)
    {
        cout<<*it<<endl;
    }
}
int main(int argc, char **argv)
{
    Mat canvas = Mat::zeros(Size(600, 400), CV_8UC3);
    srand((unsigned)time(NULL));
lable:
    int width = canvas.rows;
    int height = canvas.cols;
    int x1 = (rand() % (600 - 0))+0;
    int y1 = (rand() % (400 - 0))+0;
    int x2 = (rand() % (600 - 0))+0;
    int y2 = (rand() % (400 - 0))+0;
    int x3 = (rand() % (600 - 0))+0;
    int y3 = (rand() % (400 - 0))+0;
    int x4 = (rand() % (600 - 0))+0;
    int y4 = (rand() % (400 - 0))+0;
    double d,e,f,m;
    double Max1;
    vector<double>v1;
    Eigen::Vector3d vec1;
    Eigen::Vector3d vec2;
    Eigen::Vector3d vec3;
    Eigen::Vector3d vec4;
    Eigen::Vector3d vec5;
    Eigen::Vector3d vec6;

    Eigen::Vector3d vecline1;
    Eigen::Vector3d vecline2;
    Eigen::Vector3d vecline3;

    
    vec1 << x1, y1, 0;
    vec2 << x2, y2, 0;
    vec3 << x3, y3, 0;
    vec4 << x4, y4, 0;
    vecline1 << x2 - x1, y2 - y1, 0;
    vecline2 << x3 - x2, y3 - y2, 0;
    vecline3 << x4 - x3, y4 - y3, 0;

    if (vecline2.dot(vecline1) < 0)
    {
        if (vecline3.dot(vecline2) > 0)
        {
            if (vecline2.cross(vecline1).dot(vecline2.cross(vecline3))>0)
            {
             for (int x=0 ; x <=600; x++)
            {
                 for (int y=0 ; y <=400; y++)
               {
       Eigen::Vector3d verx1;
       verx1<<x-x1,y-y1,0;
       if (verx1.dot(vecline1)>(pow(x1-x2,2)+pow(y1-y2,2)))
       {
        d=sqrt(pow(x-x2,2) +pow(y-y2,2));  
       }
       else if (verx1.dot(vecline1)<=(pow(x1-x2,2) +pow(y1-y2,2))&&verx1.dot(vecline1)>0)
       {
        d=sqrt(pow((x-x1)*(y2-y1)+(y-y1)*(x1-x2),2)/(pow(y2-y1,2)+pow(x1-x2,2)));
       }
       else
       {
           d=sqrt(pow(x-x1,2) +pow(y-y1,2));
       }
    Eigen::Vector3d verx2;
       verx2<<x-x2,y-y2,0;

       if (verx2.dot(vecline2)>(pow(x2-x3,2)+pow(y2-y3,2)))
       {
        e=sqrt(pow(x-x3,2) +pow(y-y3,2));  
       }
       else if (verx2.dot(vecline2)<=(pow(x2-x3,2) +pow(y2-y3,2))&&verx2.dot(vecline1)>0)
       {
        e=sqrt(pow((x-x2)*(y3-y2)+(y-y2)*(x2-x3),2)/(pow(y3-y2,2)+pow(x2-x3,2)));
       }
       else
       {
           e=sqrt(pow(x-x2,2) +pow(y-y2,2));
       }
    Eigen::Vector3d verx3;
       verx3<<x-x3,y-y3,0;

       if (verx3.dot(vecline3)>(pow(x3-x4,2)+pow(y3-y4,2)))
       {
        f=sqrt(pow(x-x4,2) +pow(y-y4,2));  
       }
       else if (verx3.dot(vecline3)<=(pow(x3-x4,2) +pow(y3-y4,2))&&verx3.dot(vecline3)>0)
       {
        f=sqrt(pow((x-x3)*(y4-y3)+(y-y3)*(x3-x4),2)/(pow(y4-y3,2)+pow(x3-x4,2)));
       }
       else
       {
           f=sqrt(pow(x-x3,2) +pow(y-y3,2));
       }
    m=min({d,e,f});
    v1.push_back(m);//把m输入到v1容器中
    // canvas.at<Vec3b>(x,y)[0]=(int)(m);
    // canvas.at<Vec3b>(x,y)[1]=0;
    // canvas.at<Vec3b>(x,y)[2]=0;
    
    }
}
            double max1=find(v1); //找v1中的最大值并返回为max1
            // cout<<max1;
            //  line(canvas, Point(x1, y1), Point(x2, y2), Scalar(100, 100, 100), 4, LINE_AA, 0);
            //  line(canvas, Point(x2, y2), Point(x3, y3), Scalar(100, 100, 100), 4, LINE_AA, 0);
            //  line(canvas, Point(x3, y3), Point(x4, y4), Scalar(100, 100, 100), 4, LINE_AA, 0);
             int i =0;
             for (int r  = 0; r<=600; r++)
             {
                     for (int q  = 0; q<=400; q++)
             {
                    
                        canvas.at<Vec3b>(q,r)[0]=(int)(((v1[i])/max1)*(255));
                        canvas.at<Vec3b>(q,r)[1]=0;
                        canvas.at<Vec3b>(q,r)[2]=0;
                       i++;
                    /*对每一个像素点赋值 */    
             }
             }
             
                // canvas.at<Vec3b>(x,y)[0]=(int)(m);
                // canvas.at<Vec3b>(x,y)[1]=0;
                // canvas.at<Vec3b>(x,y)[2]=0;
             line(canvas, Point(x1, y1), Point(x2, y2), Scalar(100, 100, 100), 4, LINE_AA, 0);
             line(canvas, Point(x2, y2), Point(x3, y3), Scalar(100, 100, 100), 4, LINE_AA, 0);
             line(canvas, Point(x3, y3), Point(x4, y4), Scalar(100, 100, 100), 4, LINE_AA, 0);
             
             
             imshow("123", canvas);
             waitKey(0);   
            }
            else
            {
                goto lable;
            }

        }
        else
        {
            goto lable;
        }
    }
    else
    {
        goto lable;
    }

    
    destroyAllWindows();
    return 0;
}
